home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15112 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.8 KB

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c,comp.lang.c++
  4. Subject: Re: returning an array from function
  5. Date: 3 Apr 1996 11:11:25 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4juigtINNrus@keats.ugrad.cs.ubc.ca>
  8. References: <4jstd8$kh0@news1.sunbelt.net>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <4jstd8$kh0@news1.sunbelt.net>,
  12. Rick Huebner <bourne@infoave.net> wrote:
  13. >I have looked in several texts and looked through the faq for this question
  14. >and can't find a reference for my answer.  I am trying to return an array, or
  15.  
  16. The FAQ discusses arrays and pointers at length, something that is crucial to
  17. C. I suspect you are scanning through it for an example of an array being
  18. passed to and returned from a function. However, a careful reading will teach
  19. you that this cannot be done.
  20.  
  21. You cannot return arrays from functions nor pass them into functions. Nor can
  22. you assign directly to arrays (you can _initialize_ arrays but initialization
  23. is different from assignment). 
  24.  
  25. >a pointer to the array, from a function.  I have seen several examples of
  26. >returning a pointer, but it doesn't seem to work for me.  My function is going
  27. >to return an array of integers where 1-80 of them will be significant.  The
  28. >calling function will know how many integers are expected and will read them
  29. >off the array....If I can get the pointer back to the array.  A friend of mine
  30. >told me to just make the array a global, but I don't want to do that if I can
  31. >help it.  I would even resort to a recursive function that will pass the
  32. >integers back one at a time if that is possible.  Does anyone have any
  33. >suggestions?  I am open to anything and will go looking if you can tell me
  34. >where(hopefully online somewhere).
  35.  
  36. It's not clear what you want. If you want the function to do something to your
  37. array, why would you care about the pointer? Its value is not surprising---it
  38. is just the address of element 0.  
  39.  
  40. The only reason for wanting to pass arrays by value to a function would be to
  41. have the function operate on its own _copy_ of the array rather than the
  42. original object. C doesn't have this, but it does allow structure passing. If
  43. you make your array part of a structure, you can do it:
  44.  
  45.     struct myarray {
  46.         int x[40];
  47.     }
  48.  
  49.     int compute_something(struct myarray M)
  50.  
  51.     {
  52.         /* do something with M.x[]    */
  53.  
  54.         return 0;
  55.     }
  56.  
  57. The function compute_something() gets its own copy of the structure, and
  58. therefore its own private copy of the array to work on. This can be wasteful,
  59. especially if the function doesn't really need a private copy of the array.
  60.  
  61. If you want to protect yourself against accidentally modifying an array in a
  62. function that isn't supposed to, try using ``const'':
  63.  
  64.     int compute_something(const int arr[]);
  65.  
  66. -- 
  67.  
  68.